#include "LMK03328.h"

#define LMK_GP0_Pin GPIO_PIN_8
#define LMK_GP0_GPIO_Port GPIOF
#define LMK_GP1_Pin GPIO_PIN_9
#define LMK_GP1_GPIO_Port GPIOF
#define LMK_GP2_Pin GPIO_PIN_10
#define LMK_GP2_GPIO_Port GPIOF
#define LMK_GP3_Pin GPIO_PIN_0
#define LMK_GP3_GPIO_Port GPIOC
#define LMK_GP4_Pin GPIO_PIN_1
#define LMK_GP4_GPIO_Port GPIOC
#define LMK_GP5_Pin GPIO_PIN_2
#define LMK_GP5_GPIO_Port GPIOC
#define LMK_PDN_Pin GPIO_PIN_12
#define LMK_PDN_GPIO_Port GPIOG

uint8_t LMK03328_ADDR=0x54;

extern I2C_HandleTypeDef hi2c1;

uint8_t LMK03328_WriteReg(uint8_t addr,uint8_t data)
{
	uint8_t Txd[2];
	Txd[0]=addr;
	Txd[1]=data;
	HAL_I2C_Master_Transmit(&hi2c1,LMK03328_ADDR<<1,Txd,2,1000);
	return 0;
}

uint8_t LMK03328_ReadReg(uint8_t addr)
{
	uint8_t Txd[1];
	uint8_t Rxd[1];
	Txd[0]=addr;
	HAL_I2C_Master_Transmit(&hi2c1,LMK03328_ADDR<<1,Txd,1,1000);
	HAL_I2C_Master_Receive(&hi2c1,LMK03328_ADDR<<1,Rxd,1,1000);
	return Rxd[0];
}

uint8_t LMK03328_PowerOn(void)
{
	HAL_GPIO_WritePin(LMK_PDN_GPIO_Port,LMK_PDN_Pin,GPIO_PIN_SET);
	return 1;
}

uint8_t LMK03328_PowerDown(void)
{
	HAL_GPIO_WritePin(LMK_PDN_GPIO_Port,LMK_PDN_Pin,GPIO_PIN_RESET);
	return 1;
}

uint8_t LMK03328_TxData(uint16_t data)
{
	uint8_t Txd[2];
	Txd[0]=((data>>8)&0xFF);
	Txd[1]=(data&0xFF);
	HAL_I2C_Master_Transmit(&hi2c1,LMK03328_ADDR<<1,Txd,2,1000);
	return 1;
}

uint8_t LMK03328_LoadReg(uint16_t *Reg)
{
	int i;
	for(i=0;i<150;i++)
	{
		LMK03328_TxData(Reg[i]);
	}
	return 1;
}
